// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import 'common/stylus/index.styl'
import 'iview/dist/styles/iview.css'
import 'vant/lib/vant-css/index.css'

import Vue 				                          from 'vue'
import Vuex 			                          from 'vuex'
import App 				                          from './App'
import router 			                        from './router'
import iView 			                          from 'iView'
import VueLazy 			                        from 'vue-lazyload'
import Cookies 			                        from 'js-cookie'
import fastclick                            from 'fastclick'
import { Swipe, SwipeItem, ImagePreview, Dialog, GoodsAction, GoodsActionBigBtn, GoodsActionMiniBtn, Sku, ContactCard, ContactList, 
ContactEdit, AddressEdit, AddressList, Area, Card, Popup, Tab, Tabs }   from 'vant'


import HttpService 		from 'libs/HttpService'
import Util 			from 'libs/util'

import { Message } from 'iview'

Vue.use(iView).use(Vuex).use(Swipe).use(SwipeItem).use(Message).use(Dialog).use(GoodsAction).use(GoodsActionBigBtn).use(GoodsActionMiniBtn)
Vue.use(Sku).use(ContactCard).use(ContactList).use(ContactEdit).use(AddressEdit).use(AddressList).use(Area).use(Card).use(Popup).use(Tab).use(Tabs)

Vue.use(VueLazy, {
  loading: require('./../static/images/loading.gif')
}).use(iView)

fastclick.attach(document.body)

Vue.prototype.HttpService = HttpService
Vue.prototype.util = Util


// /* eslint-disable no-new */
// new Vue({
//   el: '#app',
//   router,
//   store,
//   components: {App},
//   template: '<App/>'
// })

let store = new Vuex.Store({
  state: {
    bookItem      : {},
    bookId        : false,
    bookDetail    : {},
    userInfo      : {},
    menuId        : 0
  },
  mutations: {
    setBookItem(state, object) {
      Cookies.set('bookItem', object)
      state.bookItem = object
    },
    setBookId(state, object) {
      Cookies.set('bookId', object)
      state.bookId = object
    },
    setBookDetail(state, object) {
      Cookies.set('bookDetail', object)
      state.bookDetail = object
    },
    setUserInfo(state, object) {
      Cookies.set('userInfo', object),
      state.userInfo = object
    },
    setMenuId(state, object) {
      state.menuId = object
    }
  }
})

router.beforeEach(function(to, from, next) {
  // console.log(Cookies.get('session'))

  Util.title(to.meta.title);

  
  if ((!Cookies.get('token')) && to.name != 'login' && to.meta.auth) { //&& !store.state.userInfo
    router.push('/home/login')
  } else {
    next()
    return
  }

  if (to.name == 'login') {
    next()
    return
  }

})


new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')